package flc.ast.activity;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.recyclerview.widget.LinearLayoutManager;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;

import com.blankj.utilcode.util.CollectionUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.lxj.xpopup.XPopup;
import com.lxj.xpopup.core.BasePopupView;
import com.lxj.xpopup.enums.PopupPosition;

import flc.ast.BaseAc;
import flc.ast.R;
import flc.ast.adapter.CurrencyAdapter;
import flc.ast.databinding.ActivityExchangeRateCalculateBinding;
import flc.ast.dialog.RateDialog;
import flc.ast.utils.Content;
import stark.common.api.StkResApi;
import stark.common.base.IReqRetCallback;
import stark.common.basic.event.EventStatProxy;
import stark.common.bean.StkExchangeRateBean;
import stark.common.bean.StkExchangeRateList;

public class ExchangeRateCalculateActivity extends BaseAc<ActivityExchangeRateCalculateBinding> {

    private CurrencyAdapter currencyAdapter;
    private BasePopupView ratePopup;
    private RateDialog rateDialog;
    private StkExchangeRateBean baseRateBean, resultRateBean;
    private int type;

    @Override
    protected int onCreate() {
        return R.layout.activity_exchange_rate_calculate;
    }

    @Override
    protected void initView() {
        EventStatProxy.getInstance().statEvent5(this, mDataBinding.event5Container);

        mDataBinding.ivExchangeRateCalculateBack.setOnClickListener(v -> onBackPressed());

        mDataBinding.tvBaseCurrency.setOnClickListener(this);
        mDataBinding.tvResultCurrency.setOnClickListener(this);
        mDataBinding.ivExchange.setOnClickListener(this);

        mDataBinding.rvCurrencyList.setLayoutManager(new LinearLayoutManager(mContext, LinearLayoutManager.HORIZONTAL, false));
        currencyAdapter = new CurrencyAdapter();
        mDataBinding.rvCurrencyList.setAdapter(currencyAdapter);
        currencyAdapter.setOnItemClickListener(this);

        mDataBinding.etInput.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                long amount = 0;
                if (s.toString().equals("")) {
                    amount = 0;
                } else {
                    amount = Long.valueOf(s.toString());
                }
                calculate(amount);
            }
        });
    }

    @Override
    protected void onClickCallback(View v) {
        switch (v.getId()) {
            case R.id.tvBaseCurrency://基础货币
                type = 0;
                showRateDialog();
                break;
            case R.id.tvResultCurrency://转换货币
                type = 1;
                showRateDialog();
                break;
            case R.id.ivExchange://交换
                StkExchangeRateBean bean = baseRateBean;
                baseRateBean = resultRateBean;
                resultRateBean = bean;
                mDataBinding.tvBaseCurrency.setText(baseRateBean.name);
                mDataBinding.tvBaseUnit.setText(baseRateBean.code);
                mDataBinding.tvResultCurrency.setText(resultRateBean.name);
                mDataBinding.tvResultUnit.setText(resultRateBean.code);
                String str = mDataBinding.etInput.getText().toString();
                long amount;
                if (str.equals("")) {
                    amount = 0;
                } else {
                    amount = Long.valueOf(str.toString());
                }
                calculate(amount);
                break;
        }
    }

    @Override
    protected void initData() {
        showDialog(getString(R.string.loading));
        StkResApi.getMoneyRateList(this, new IReqRetCallback<StkExchangeRateList>() {
            @Override
            public void onResult(boolean success, String msg, @Nullable StkExchangeRateList result) {
                if (success) {
                    if (!CollectionUtils.isEmpty(result.rateList)) {
                        currencyAdapter.setList(result.rateList);
                        baseRateBean = result.rateList.get(0);
                        resultRateBean = result.rateList.get(1);
                        mDataBinding.tvBaseCurrency.setText(baseRateBean.name);
                        mDataBinding.tvBaseUnit.setText(baseRateBean.code);
                        mDataBinding.tvResultCurrency.setText(resultRateBean.name);
                        mDataBinding.tvResultUnit.setText(resultRateBean.code);
                        dismissDialog();
                    }
                } else {
                    dismissDialog();
                    ToastUtils.showShort(R.string.query_failure_tips);
                }
            }
        });
    }

    private void calculate(long amount) {
        double exchangeValue = (amount / baseRateBean.rate) * resultRateBean.rate;
        String result = String.format("%.4f", exchangeValue);
        mDataBinding.tvResult.setText(result);
    }

    //选择货币弹框
    private void showRateDialog() {
        if (rateDialog == null) rateDialog = new RateDialog(mContext);
        ratePopup = new XPopup.Builder(mContext).popupPosition(PopupPosition.Bottom) //手动指定弹窗的位置
                .isDestroyOnDismiss(false) //对于只使用一次的弹窗，推荐设置这个
                .atView(type == 0 ? mDataBinding.tvBaseCurrency : mDataBinding.tvResultCurrency).hasShadowBg(false) // 去掉半透明背景
                .asCustom(rateDialog);
        rateDialog.setListener(new RateDialog.Ilistener() {

            @Override
            public void click(StkExchangeRateBean bean) {
                if (type == 0) {
                    baseRateBean = bean;
                    mDataBinding.tvBaseCurrency.setText(bean.name);
                    mDataBinding.tvBaseUnit.setText(bean.code);
                } else {
                    resultRateBean = bean;
                    mDataBinding.tvResultCurrency.setText(bean.name);
                    mDataBinding.tvResultUnit.setText(bean.code);
                }
                ratePopup.dismiss();
            }
        });
        ratePopup.show();
    }

}